<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of History
 *
 * @author Laszlo.Pokoradi
 */
class History extends Model {
  public function  __construct() {
    parent::Model();
  }

  public function create($data) {
    foreach ($accountData as $key => $value) {
      $this->db->set($key, $value);
    }
    $query = $this->db->insert(HISTORY_TABLENAME);

    if(!$query) {
      log_message('error','Error during history creation! SQL: '.$this->db->insert_string());
      return FALSE;
    } else {
      $id = $this->db->insert_id();
      log_message('info','History ('.$id.') was inserted to the database!');
      return $id;
    }
  }

  public function read($id) {
    $this->db->where('id',$id);
    $query = $this->db->insert(HISTORY_TABLENAME);

    if(!$query || $query->num_rows() != 1) {
      log_message('error','Error during history read! SQL: '.$this->db->last_query());
      return FALSE;
    } else {
      log_message('info','History ('.$id.') was read from the database!');
      return $query->row_array();
    }
  }

  public function update($id, $data) {
    foreach ($data as $key => $value) {
      $this->db->set($key, $value);
    }
    $this->db->where('id',$id);
    $query = $this->db->update(HISTORY_TABLENAME);

    if(!$query || $this->db->affected_rows() != 1) {
      log_message('error','Error during history update! SQL: '.$this->db->update_string());
      return FALSE;
    } else {
      log_message('info','History ('.$id.') was updated in the database!');
      return TRUE;
    }
  }

  public function delete($id) {
    $this->db->where('id', $id);
    $query = $this->db->delete(HISTORY_TABLENAME);

    if(!$query || $this->db->affected_rows() != 0) {
      log_message('error','Error during history delete! SQL: '.$this->db->last_query());
      return FALSE;
    } else {
      log_message('info','History ('.$id.') was deleted from the database!');
      return TRUE;
    }
  }
}
?>
